
/**
 * 记录因未登录而离开的页面
 */
import type { RouteName } from '../modules/route'
interface routeType {
  name: RouteName,
  params: {[key:string]:any},
  query: {[key:string]:any},
}
export let unlLoginPage:routeType = {
  name: 'Home',
  params: {},
  query: {}
}
export function setUnlLoginPage(route:routeType) {
  unlLoginPage = route
}

/* 比赛数据 */
import { getAllCompetition } from '@/api/competition'
import type { CompetitionType } from '@/modules/competition'
let competitions:CompetitionType[] = []

export async function getCompetitions() {
  if (competitions.length === 0) {
    const res = await getAllCompetition()

    competitions = res.data.pageData
  }

  return JSON.parse(JSON.stringify(competitions))
}
/**
 * 根据key val值查找比赛
 */
export function findCompetition(val:string, key = 'id') {
  return competitions.find(item => item[ key ] === val) || null
}

/* 课程数据 */
import { getCourses } from '@/api/course.js'
const courses = {}

export function getCoursesData(type:number) {
  if (!courses[ type ]) {
    return getCourses(type)
      .then((res:any) => {
        courses[ type ] = res
        return courses[ type ]
      })
  }

  return JSON.parse(JSON.stringify(courses[ type ]))
}
